var databaseName = "alopex";

function init() {
	_initHeader();
	
	var array = new Array();
	
	array.push("excuteQuery (Create Table)");
	array.push("excuteQuery (Insert)");
	array.push("excuteQuery (Update)");
	array.push("excuteQuery (Delete)");
	array.push("insert");
	array.push("update");
	array.push("delete");
	array.push("select");
	array.push("commit");
	array.push("error query");
	
	draw_list(array);
}

function setOnListClickHandler(idx) {
	switch(idx) {
		case 0 :
			testExecQueryCreateTable();
			
		break;
		
		case 1 :
			testExecQueryInsert();
			
		break;
		
		case 2 :
			testExecQueryUpdate();
			
		break;
		
		case 3 :
			testExecQueryDelete();
			
		break;
		
		case 4 :
			testInsert();
			
		break;
		
		case 5 :
			testUpdate();
			
		break;
		
		case 6 :
			testDelete();
			
		break;
		
		case 7 :
			testSelect();
			
		break;
		
		case 8 :
			testCommit();
			
		break;
		
		case 9 :
			testErrorQuery();
			
		break;
		
		default :

		break;
	}
}

function testExecQueryCreateTable() {
	alert("Commit 후 테이블이 생성됩니다.");
	
	var args = new Array();
	var query = {
		"sql" : "CREATE TABLE IF NOT EXISTS alopex_test_table(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL DEFAULT \"\", name TEXT, content TEXT);",
		"args" : args
	};
	database.execQuery(query);
}

function testExecQueryInsert() {
	var title, name, content;
	var args = new Array();
	
	title = prompt("Insert title", "");
	if(title == null)
		return;
	
	name = prompt("Insert name", "");
	if(name == null)
		return;
	
	content = prompt("Insert content", "");
	if(content == null)
		return;
	
	args[0] = title;
	args[1] = name;
	args[2] = content;
	
	var query = {
		"sql" : "INSERT INTO alopex_test_table(title, name, content) VALUES(?, ?, ?);",
		"args" : args
	};
	database.execQuery(query);
}

function testExecQueryUpdate() {
	var title, name, content, id;
	var args = new Array();
	
	title = prompt("Insert title", "");
	if(title == null)
		return;
	
	name = prompt("Insert name", "");
	if(name == null)
		return;
	
	content = prompt("Insert content", "");
	if(content == null)
		return;
	
	id = prompt("Insert id", "");
	if(id == null)
		return;
	
	args[0] = title;
	args[1] = name;
	args[2] = content;
	args[3] = id;
	
	var query = {
		"sql" : "UPDATE alopex_test_table SET title = ?, name = ?, content = ? where id = ?;",
		"args" : args
	};
	database.execQuery(query);
}

function testExecQueryDelete() {
	var id;
	var args = new Array();
	
	id = prompt("Insert id", "");
	
	if(id) {
		args[0] = id;
		
		var query = {
			"sql" : "DELETE FROM alopex_test_table where id = ?;",
			"args" : args
		};
		database.execQuery(query);
	}
}

function testInsert() {
	var title, name, content;
	
	title = prompt("Insert title", "");
	if(title == null)
		return;
	
	name = prompt("Insert name", "");
	if(name == null)
		return;
	
	content = prompt("Insert content", "");
	if(content == null)
		return;
	
	var query = {
		"tableName" : "alopex_test_table",
		"insertValues" : {
			"title" : title,
			"name" : name,
			"content" : content
		}
	};
	
	database.insert(query);
}

function testUpdate() {
	var id, title, name, content;
	var args = new Array();
	
	title = prompt("Insert title", "");
	if(title == null)
		return;
	
	name = prompt("Insert name", "");
	if(name == null)
		return;
	
	content = prompt("Insert content", "");
	if(content == null)
		return;
	
	id = prompt("Insert id", "");
	if(id == null)
		return;
	
	args[0] = id;
	
	var query = {
		"tableName" : "alopex_test_table",
		"whereClause" : "id=?",
		"args" : args,
		"updateValues" : {
			"title" : title,
			"name" : name,
			"content" : content
		}
	};
	database.update(query);
}

function testDelete() {
	var id;
	var args = new Array();
	
	id = prompt("Insert id", "");
	if(id == null)
		return;
	
	args[0] = id;
	
	var query = {
		"tableName" : "alopex_test_table",
		"whereClause" : "id=?",
		"args" : args
	};
	database.deleteRow(query);
}

function testSelect() {
	var args = new Array();
	var query = {
		"sql" : "SELECT * FROM alopex_test_table;",
		"args" : args
	};
	
	database.select(databaseName, query, successCallback, selectErrorCallback);
}

function testCommit() {
	database.commit(databaseName, successCallback, errorCallback);
}

function successCallback(result) {
	var message = "_______select result_______\n";
	
	if(result != null) {
		for( var i = 0; i < result.length; i++) {
			if(result[i].id != null) {
				message = message + "id = " + result[i].id + " title = " + result[i].title + " name = " + result[i].name + " content = " + result[i].content + "\n";
			} else {
				alert("Queries are excuted.");
			}
		}
	} else if(result == null) {
		alert("Result value is null.");
	}
	
	if(message != "_______select result_______\n") {
		alert(message);
	}
}

function selectErrorCallback(errorMessage) {
	notifyError("select", errorMessage);
}

function errorCallback(errorMessage) {
	notifyError("conmmit", errorMessage);
}

function testErrorQuery() {
	var query = {
		"tableName" : "alopex_no_table",
		"insertValues" : {
			"title" : "testErrorQuery",
			"name" : "testErrorQuery",
			"content" : "testErrorQuery"
		}
	};
	
	database.insert(query);
}